package com.example.graduatetest.view.model;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.widget.Toolbar;
import androidx.lifecycle.ViewModel;

import com.example.graduatetest.R;
import com.example.graduatetest.bean.Classes;
import com.example.graduatetest.bean.Notice;
import com.example.graduatetest.callback.ClassCallBack;
import com.example.graduatetest.callback.StringCallBack;
import com.example.graduatetest.databinding.ActivityNoticeDetailBinding;
import com.example.graduatetest.model.CardModel;
import com.example.graduatetest.model.ClassModel;
import com.example.graduatetest.model.NoticeModel;

public class NoticeDetailViewModel extends ViewModel {
    private Activity activity;
    private ActivityNoticeDetailBinding binding;
    private Notice ownNotice;
    private TextView textContent;
    private Handler handler = new Handler();
    private int num = 0;
    private String kk;
    private String rr;
    public void init(Activity activity,ActivityNoticeDetailBinding binding,Notice ownNotice){
        this.activity = activity;
        this.binding = binding;
        this.ownNotice = ownNotice;
        textContent = binding.textContent;
        toolbarInit();
        initClick();
        initView();
    }

    //初始化toolbar
    private void toolbarInit(){
        Toolbar toolbar = binding.toolbar;
        toolbar.setTitle("通知详情");
        toolbar.setNavigationIcon(R.drawable.ic_arrow_back_black_24dp);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                activity.onBackPressed();
            }
        });
    }
    ///初始化页面
    private void initView(){
        binding.textTitle.setText(ownNotice.getTitle());
        binding.textResult.setVisibility(View.GONE);
        binding.textType.setText("类型："+ownNotice.getLabel());
        binding.textSendPerson.setText(ownNotice.getSend_name());
        binding.textClassNumber.setText("课程号："+ownNotice.getClass_number());
        binding.textClassCode.setText("签到码："+ownNotice.getClass_code());
        binding.textTime.setText("发布时间："+ownNotice.getTime());
        binding.textContent.setText(ownNotice.getContent());
        num = textContent.getText().length();
        binding.textTextNumber.setText("" + num+"/500");

        if(ownNotice.getStatus().equals("已处理")){
            binding.textResult.setText("已处理");
            binding.textResult.setVisibility(View.VISIBLE);
        }

        if(!ownNotice.getLabel().equals("请假") || ownNotice.getStatus().equals("已处理")){
            binding.submitButton.setVisibility(View.GONE);
            binding.lyOther.setVisibility(View.GONE);
        }


    }

    private void initClick(){
        binding.submitButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CardModel.askCard(ownNotice.getSend_number(), ownNotice.getClass_number(), ownNotice.getClass_code(), new StringCallBack() {
                    @Override
                    public void onSuccess(String result) {
                        kk = result;
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                if(kk.equals("success")){
                                    Toast.makeText(activity,"处理成功", Toast.LENGTH_SHORT).show();
                                }else{
                                    Toast.makeText(activity,"处理失败", Toast.LENGTH_SHORT).show();
                                }
                            }
                        });

                    }

                    @Override
                    public void onFailed() {
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(activity,"网络错误", Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                });

                NoticeModel.updateNoticeStatus(ownNotice.getId(), new StringCallBack() {
                    @Override
                    public void onSuccess(String result) {
                        rr = result;
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                if(rr.equals("success")){
                                    activity.onBackPressed();
                                    Toast.makeText(activity,"处理成功", Toast.LENGTH_SHORT).show();
                                }else{
                                    Toast.makeText(activity,"处理失败", Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                    }

                    @Override
                    public void onFailed() {
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(activity,"网络错误", Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                });
            }
        });
    }



}
